#pragma once
#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class K>
struct DefaultHashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

template<>
struct DefaultHashFunc<string>
{
	size_t operator()(const string& str)
	{
		// BKDR
		size_t hash = 0;
		for (auto ch : str)
		{
			hash *= 131;
			hash += ch;
		}

		return hash;
	}
};



namespace open_hash
{
enum STATE
{
	EXIST,
	EMPTY,
	DELETE
};
template<class K,class V>
struct HashData
{
	pair<K, V> _kv;
	STATE _state = EMPTY;
};

template<class K, class V,class HashFunc=DefaultHashFunc<K>>
class HashTable
{
public:
	HashTable()
	{
		_table.resize(10);
	}

	bool Insert(const pair<K, V>& kv)
	{
		if (Find(kv.first))
		{
			return false;
		}

		//检查负载因子
		if (_n * 10 / _table.size() >= 7)
		{
			int newsize = _table.size() * 2;
			HashTable newtable;
			newtable._table.resize(newsize);
			//把旧表的数据插入到新表
			for (int i = 0; i < _table.size(); i++)
			{
				if (_table[i]._state == EXIST)
				{
					newtable.Insert(_table[i]._kv);
				}
			}

			//交换两个表
			_table.swap(newtable._table);

		}
		HashFunc hf;
		size_t hashi = hf(kv.first) % _table.size();
		//线性探测
		if (_table[hashi]._state == EXIST)
		{
			hashi++;
			hashi %= _table.size();
		}

		_table[hashi]._kv = kv;
		_table[hashi]._state = EXIST;
		_n++;
		return true;
	}

	HashData<const K, V>* Find(const K& key)
	{
		HashFunc hf;
		size_t hashi = hf(key) % _table.size();
		//线性探测
		while (_table[hashi]._state != EMPTY)
		{
			if (_table[hashi]._state == EXIST && _table[hashi]._kv.first == key)
			{
				return (HashData<const K, V>*) & _table[hashi];
			}
			hashi++;
			hashi %= _table.size();
		}

		return nullptr;
		
	}

	bool Erase(const K& key)
	{
		auto ret = Find(key);
		if (ret)
		{
			ret->_state = DELETE;
			_n--;
			return true;
		}
		return false;
		
	}
private:
	vector<HashData<K,V>> _table;
	size_t _n = 0;  //有效数据的个数
};
}

namespace hash_bucket
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;

		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};

	//前置声明
	template<class K, class T, class KeyOfT, class HashFunc>
	class HashTable;
	
	template<class K, class T, class Ref,class Ptr, class KeyOfT, class HashFunc>
	struct HTIterator
	{
		typedef HashNode<T> Node;
		typedef HTIterator<K, T, Ref, Ptr, KeyOfT, HashFunc> Self;
		typedef HTIterator<K, T, T&, T*, KeyOfT, HashFunc> iterator;

		Node* _node;
		//解决权限扩大的问题
		const HashTable<K, T, KeyOfT, HashFunc>* _pht;

		//解决权限扩大的问题
		HTIterator(Node* node, const HashTable<K, T, KeyOfT, HashFunc>* pht)
			:_node(node)
			,_pht(pht)
		{}

		//普通迭代器--拷贝构造
		//const迭代器--构造
		HTIterator(const iterator& it)
			:_node(it._node)
			,_pht(it._pht)
		{

		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &(_node->_data);
		}

		Self& operator++()
		{
			if (_node->_next)
			{
				_node = _node->_next;
			}
			else
			{
				//找到下一个桶
				KeyOfT kot;
				HashFunc hf;

				size_t hashi = hf(kot(_node->_data))%_pht->_table.size();
				//从下一个位置开始找
				hashi++;
				while (hashi < _pht->_table.size())
				{
					if (_pht->_table[hashi])
					{
						_node = _pht->_table[hashi];
						return *this;
					}
					else
					{
						hashi++;
					}
				}
					_node = nullptr;
			}

			return *this;
		}

		bool operator!=(const Self& s)
		{
			return _node!= s._node;
		}
	};


	template<class K, class T, class KeyOfT, class HashFunc = DefaultHashFunc<K>>
	class HashTable
	{
	public:
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class HashFunc>
		friend struct HTIterator;

		typedef HashNode<T> Node;
		typedef HTIterator<K, T, T&, T*, KeyOfT, HashFunc> iterator;
		typedef HTIterator<K, T, const T&, const T*, KeyOfT, HashFunc> const_iterator;


		iterator begin()
		{
			//找第一个桶
			for (size_t i = 0; i < _table.size(); i++)
			{
				Node* cur = _table[i];
				if (cur)
				{
					return iterator(cur, this);
				}
			}

			return iterator(nullptr, this);
		}

		iterator end()
		{
			return iterator(nullptr, this);
		}

		const_iterator begin() const
		{
			//找第一个桶
			for (size_t i = 0; i < _table.size(); i++)
			{
				Node* cur = _table[i];
				if (cur)
				{
					return const_iterator(cur, this);
				}
			}

			return const_iterator(nullptr, this);
		}

		const_iterator end() const
		{
			return const_iterator(nullptr, this);
		}

		HashTable()
		{
			_table.resize(10, nullptr);
		}

		~HashTable()
		{
			for (size_t i = 0; i < _table.size(); i++)
			{
				Node* cur = _table[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_table[i] = nullptr;

			}
		}

		HashTable(const HashTable<K, T, KeyOfT>& h)
			:_n(h._n)
		{
			_table.resize(h._table.size(),nullptr);
			for (int i = 0; i < h._table.size(); i++)
			{
				Node* cur = h._table[i];
				while (cur)
				{
					Node* newnode = new Node(cur->_data);
					newnode->_next = _table[i];
					_table[i] = newnode;
					cur = cur->_next;
				}
			}
		}

		void swap(HashTable<K, T, KeyOfT>& h1)
		{
			std::swap(h1._n, _n);
			std::swap(h1._table, _table);
		}

		HashTable<K, T, KeyOfT>& operator=(HashTable<K,T,KeyOfT> h)
		{
			swap(h);
			return *this;
		}


		pair<iterator,bool> Insert(const T& data)
		{
			KeyOfT kot;

			iterator it = Find(kot(data));
			if (it != end())
			{
				return make_pair(it, false);
			}

			HashFunc ht;

			//负载因子为1就扩容
			if (_n == _table.size())
			{
				size_t newsize = _table.size() * 2;
				//创建新表
				vector<Node*> newtable;
				newtable.resize(newsize, nullptr);

				//遍历旧表，顺手牵羊，插入到新表
				for (int i = 0; i < _table.size(); i++)
				{
					Node* cur = _table[i];
					while (cur)
					{
						size_t hashi = ht(kot(cur->_data)) % newtable.size();
						Node* next = cur->_next;
						cur->_next = newtable[hashi];
						newtable[hashi] = cur;
						cur = next;
					}
					_table[i] = nullptr;
				}

				//交换新旧表
				_table.swap(newtable);
			}

			size_t hashi = ht(kot(data)) % _table.size();
			//头插
			Node* newnode = new Node(data);
			newnode->_next = _table[hashi];
			_table[hashi] = newnode;
			_n++;
			return make_pair(iterator(newnode,this), true);

		}

		iterator Find(const K & key)
		{
			HashFunc ht;
			KeyOfT kot;

			size_t hashi = ht(key) % _table.size();
			Node* cur = _table[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					return iterator(cur, this);
				}

				cur = cur->_next;
			}

			return iterator(nullptr, this);
		}

		bool Erase(const K& key)
		{
			HashFunc ht;
			KeyOfT kot;

			size_t hashi = ht(key) % _table.size();
			Node* cur = _table[hashi];
			Node* prev = nullptr;

			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					if (prev == nullptr)
					{
						_table[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;
					--_n;
					return true;
				}

				prev = cur;
				cur = cur->_next;
			}

			return false;
		}

		void Print()
		{
			for (int i = 0; i < _table.size(); i++)
			{
				KeyOfT kot;

		    	Node* cur = _table[i];
				printf("[%d]:", i);
				while (cur)
				{
					cout << kot(cur->_data) << "->";
					cur = cur->_next;
				}
				printf("NULL\n");
			}
		}
		private:
			vector<Node*> _table;
			size_t _n = 0;
		};
}